package com.food.platformmsgmanagement.application;

import com.food.platformmsgmanagement.adapter.inbound.AlimentDTO;
import com.food.platformmsgmanagement.adapter.inbound.CommentDTO;
import com.food.platformmsgmanagement.domain.Aliment;
import com.food.platformmsgmanagement.domain.Comment;
import com.food.platformmsgmanagement.domain.Status;
import com.food.platformmsgmanagement.port.inbound.AlimentUseCase;
import com.food.platformmsgmanagement.port.outbound.AlimentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AlimentApplicationService implements AlimentUseCase {
    private final AlimentRepository repository;

    @Autowired
    public AlimentApplicationService(AlimentRepository repository) {
        this.repository = repository;
    }

    @Override
    public List<AlimentDTO> getAllAliment() {
        return repository.queryAllAliment();
    }

    @Override
    public AlimentDTO getTheAliment(String id) {
        return repository.queryOneAliment(id);
    }

    @Override
    public void changeAlimentStatus(String id, String status) {
        repository.updateAlimentState(id, status);
    }

    @Override
    public String addAliment(String title, String mainText) {
        Aliment aliment = new Aliment(title, mainText, new ArrayList<>());
        repository.addAliment(aliment);
        return aliment.id();
    }

    @Override
    public void addAlimentImg(String id, List<String> urlList) {
        AlimentDTO dto = repository.queryOneAliment(id);
        List<String> res = dto.getImgList();
        res.addAll(urlList);
        repository.updateAlimentImg(res.toString().substring(1,res.toString().length()-1), dto.getId());
    }

    @Override
    public boolean deleteAliment(String id) {
        return repository.deleteAliment(id);
    }

    @Override
    public List<CommentDTO> getAllComment(String alimentId) {
        return repository.queryAllComment(alimentId);
    }

    @Override
    public List<CommentDTO> getNumComment(String alimentId, int number) {
        return repository.queryNumComment(alimentId, number);
    }

    @Override
    public void addComment(String alimentId, String userName, String mainText) {
        Comment comment = new Comment(alimentId, userName, mainText);
        repository.addComment(comment);
    }

    @Override
    public boolean deleteComment(String id) {
        return repository.deleteComment(id);
    }
}
